package com.weixing.mall.provider.support.excalservice;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.model.query.ProductStockQuery;
import com.weixing.mall.provider.model.vo.ProductStockListVo;
import com.weixing.mall.provider.service.IProductStockService;
import com.weixing.mall.provider.support.excaldata.ProductStockED;
import com.weixing.mall.util.SKUUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 出库单导入数据处理
 *
 * @date 2020-6-6
 */
public class ProductStockDataListener extends AnalysisEventListener<ProductStockED> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductStockDataListener.class);
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 5;

    List<ProductStockED> list = new ArrayList<ProductStockED>();

    private IProductStockService productStockService;
    /**
     * 业务数据组装
     */
    private List<ProductStockListVo> returnList;

    /**
     * 业务参数
     */
    private Long depotId;

    private ProductStockDataListener() {

    }

    public ProductStockDataListener(Long depotId,IProductStockService productStockService) {
        this.depotId = depotId;
        this.productStockService = productStockService;
    }

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param productStockED
     * @param analysisContext
     */
    @Override
    public void invoke(ProductStockED productStockED, AnalysisContext analysisContext) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(productStockED));
        list.add(productStockED);
    }


    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        LOGGER.info("{}条数据，开始处理数据！", list.size());
        //返回数据收集
        List<ProductStockListVo> loadList = Lists.newArrayList();
        //查询仓库数据
        ProductStockQuery psq = new ProductStockQuery();
        psq.setDepotId(depotId);
        List<ProductStockListVo> listVo = productStockService.selectList(psq);
        //判断数据是否为空
        if(CollUtil.isNotEmpty(list) && CollUtil.isNotEmpty(listVo)){
            //匹配数据  条件货号、款号、颜色、尺码必须相同 数量必须大于导入的匹配数量
            for (ProductStockListVo pslv : listVo) {
                //数据条件校验任何参数必须有值
                if (StrUtil.isEmpty(pslv.getGoodsSn())
                        || StrUtil.isEmpty(pslv.getStyleNo())
                        || StrUtil.isEmpty(pslv.getProductSkuDesp())
                        || pslv.getStock() == null) {
                    continue;
                }
                String color = "";
                String size = "";
                //sku拆分颜色
                String[] skuDesp = pslv.getProductSkuDesp().split(",");
                if(skuDesp.length == 2){
                    color = SKUUtils.color2(pslv.getProductSkuDesp());
                    size = SKUUtils.size2(pslv.getProductSkuDesp());
                }else {
                    continue;
                }
                //判断颜色尺码是否为空
                if(StrUtil.isEmpty(color)||StrUtil.isEmpty(size)){
                    continue;
                }
                //循环匹配数据
                Iterator<ProductStockED> it = list.iterator();
                while (it.hasNext()){
                    ProductStockED pse = it.next();
                    if(StrUtil.isEmpty(pse.getGoodsSn())
                            ||StrUtil.isEmpty(pse.getStyleNo())
                            ||StrUtil.isEmpty(pse.getColor())
                            ||StrUtil.isEmpty(pse.getSize())
                            ||pse.getQuantity()==null){
                        continue;
                    }
                    //比对条件
                    if(pse.getGoodsSn().equals(pslv.getGoodsSn())
                            &&pse.getStyleNo().equals(pslv.getStyleNo())
                            &&pse.getColor().equals(color)
                            &&pse.getSize().equals(size)
                            &&pslv.getStock() >= pse.getQuantity()){
                        //补充导入Excal的库存数量
                        pslv.setExcalQuantity(pse.getQuantity());
                        //匹配成功装载数据
                        loadList.add(pslv);
                        //移除匹配成功数据
                        it.remove();
                        break;//跳出此循环
                    }
                }
            }
        }
        returnList = loadList;
        LOGGER.info("{}条数据，数据封装处理完成 ", returnList.size());
    }

    /**
     * 获取处理结果数据
     */
    public List<ProductStockListVo> getReturnList(){
        return this.returnList;
    }
}
